package ch.fhnw.kosmonauten.client.gui;

import java.util.Locale;

import javax.swing.JOptionPane;

import ch.fhnw.kosmonauten.client.net.ServerConnection;
import ch.fhnw.kosmonauten.model.User;
import ch.fhnw.kosmonauten.net.Request;
import ch.fhnw.kosmonauten.net.Response;
import ch.fhnw.kosmonauten.net.TextMessage;

/**
*
* @author Oliver Freiermuth
*/
public class AdminUserFrame extends javax.swing.JFrame {

   /**
    * Creates new form MaintainUser
    */
   public AdminUserFrame(User user) {
       this.user = user;
       this.toFront();
       this.setAlwaysOnTop(true);
	   initComponents();
       this.setVisible(true);
   }

   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">
   private void initComponents() {

       buttonGroup1 = new javax.swing.ButtonGroup();
       buttonGroup2 = new javax.swing.ButtonGroup();
       buttonGroup3 = new javax.swing.ButtonGroup();
       buttonGroup4 = new javax.swing.ButtonGroup();
       jButton1 = new javax.swing.JButton();
       jLabel1 = new javax.swing.JLabel();
       jLabel2 = new javax.swing.JLabel();
       jLabel3 = new javax.swing.JLabel();
       jLabel4 = new javax.swing.JLabel();
       jTextField1 = new javax.swing.JTextField();
       jRadioButton1 = new javax.swing.JRadioButton();
       jRadioButton2 = new javax.swing.JRadioButton();
       jRadioButton3 = new javax.swing.JRadioButton();
       jRadioButton4 = new javax.swing.JRadioButton();
       jTextField3 = new javax.swing.JTextField();
       jButton2 = new javax.swing.JButton();
       jComboBox1 = new javax.swing.JComboBox();
       
    // Kategorien aus der Datenbank holen
    	sc = new ServerConnection();
    	Response resp = sc.sendRequest(new Request(new TextMessage("getUsers"), "getUsers"));
    	if (resp == null)
    		new ErrorDialog(user.displayValue("mw40"));
    	else if (resp.getContent() instanceof User[]) {
    		users = (User[]) resp.getContent();
    		if (users.length == 0)
    			new ErrorDialog(user.displayValue("mw134"));
    		else {
    			jComboBox1 = new javax.swing.JComboBox();
    			for (User user : users)
    			{
    				jComboBox1.addItem(new String("[" + user.getId() + "] - " + user.getUsername()));
    			}
    			
    		}
    	} else
    		new ErrorDialog(resp.getContent().toString());
       
       jButton1.setText(user.displayValue("mw126"));
       jButton1.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton1ActionPerformed(evt);
           }
       });

       jButton2.setText(user.displayValue("mw127"));
       jButton2.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton2ActionPerformed(evt);
           }
       });

       setTitle("User bearbeiten (Admin)");

       jButton1.setText(user.displayValue("mw126"));
       jButton1.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton1ActionPerformed(evt);
           }
       });
       
       jLabel1.setText(user.displayValue("mw108"));

       jLabel2.setText(user.displayValue("mw109"));

       jLabel3.setText(user.displayValue("mw110"));

       jLabel4.setText(user.displayValue("mw111"));

       jTextField1.setText("");

       buttonGroup2.add(jRadioButton1);
       jRadioButton1.setText(user.displayValue("mw112"));

       buttonGroup2.add(jRadioButton2);
       jRadioButton2.setText(user.displayValue("mw113"));

       buttonGroup1.add(jRadioButton3);
       jRadioButton3.setText(user.displayValue("mw114"));

       buttonGroup1.add(jRadioButton4);
       jRadioButton4.setText(user.displayValue("mw115"));

       jTextField3.setText("");

       jButton2.setText(user.displayValue("mw127"));
       jButton2.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton2ActionPerformed(evt);
           }
       });

       //jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

       javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
       getContentPane().setLayout(layout);
       layout.setHorizontalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(layout.createSequentialGroup()
               .addContainerGap()
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                   .addGroup(layout.createSequentialGroup()
                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addComponent(jLabel4)
                           .addComponent(jLabel3))
                       .addGap(23, 23, 23)
                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addComponent(jRadioButton4)
                           .addComponent(jRadioButton3)
                           .addComponent(jRadioButton1)
                           .addComponent(jRadioButton2)))
                   .addGroup(layout.createSequentialGroup()
                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addComponent(jLabel1)
                           .addComponent(jLabel2))
                       .addGap(18, 18, 18)
                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                           .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                           .addComponent(jTextField3)))
                   .addGroup(layout.createSequentialGroup()
                       .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                       .addGap(18, 18, 18)
                       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                   .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
               .addGap(0, 20, Short.MAX_VALUE))
       );
       layout.setVerticalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
               .addGap(16, 16, 16)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jLabel1)
                   .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jLabel2)
                   .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addGap(12, 12, 12)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jLabel4)
                   .addComponent(jRadioButton3))
               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
               .addComponent(jRadioButton4)
               .addGap(10, 10, 10)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jLabel3)
                   .addComponent(jRadioButton1))
               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
               .addComponent(jRadioButton2)
               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
               .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addGap(18, 18, 18)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jButton2)
                   .addComponent(jButton1))
               .addContainerGap())
       );

       pack();
   }// </editor-fold>                                       

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	   if (jTextField1.getText().length() > 0 && 
				jTextField3.getText().length() > 0 && 
				(jRadioButton1.isSelected() || jRadioButton2.isSelected()) && 
				(jRadioButton3.isSelected() || jRadioButton4.isSelected())) 
		{
		   	boolean nextstep = true;
	   		String tempname = jTextField1.getText();
	   		
		    	for (int i = 0; i < jComboBox1.getItemCount();i++)
		    	{
		    		if (tempname.equalsIgnoreCase(jComboBox1.getItemAt(i).toString().split("\\]")[1].substring(3)))
		    		{
		    			nextstep = false;
		    			new ErrorDialog(14, JOptionPane.INFORMATION_MESSAGE, user);
		    			break;
		    		}
		    	}
		        	
		    	if (nextstep)
		    	{
		    		user.setUsername(jTextField1.getText());
		    		user.setPassword(jTextField3.getText());
		    		if (jRadioButton1.isSelected()){
		    			user.setRole("user");
		    		}
		    		else{
		    			user.setRole("admin");
		    		}
		    		if (jRadioButton3.isSelected()){
		    			user.setLanguage(Locale.GERMAN);
		    		}
		    		else{
		    			user.setLanguage(Locale.ENGLISH);
		    		}
			
		    		// User profil speichern
		    		sc = new ServerConnection();
		    		Response resp = sc.sendRequest(new Request(this.user,"createUserProfile"));
		    		if (resp == null)
		    			System.out.println(user.displayValue("mw82"));
		    		else if (resp.getContent() instanceof Boolean) 
		    		{
		    			boolean ok = (Boolean) resp.getContent();
		    			if (ok)
		    			{
		    				this.setVisible(false);
		    			}
		    			else
		    			{
		    				new ErrorDialog(4, JOptionPane.ERROR_MESSAGE, user);
		    			}
		    		}
		    		else if (resp.getContent() == null )
		    		{
		    			new ErrorDialog(5, JOptionPane.ERROR_MESSAGE, user);
		    		}
		    		else
		    			System.out.println(resp.getContent().toString());
			
		    	} 
		}
		    	else
		    		new ErrorDialog(6, JOptionPane.INFORMATION_MESSAGE, user);
	   }
//Hier schauen oben in den Klammern falls nicht funktzt
   private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
	   
	   if (jTextField1.getText().length() > 0 && jTextField3.getText().length() > 0 && 
				(jRadioButton1.isSelected() || jRadioButton2.isSelected()) && 
				(jRadioButton3.isSelected() || jRadioButton4.isSelected())){
   		boolean nextstep = true;
   		String tempname = jTextField1.getText();
   		
	    	for (int i = 0; i < jComboBox1.getItemCount();i++)
	    	{
	    		if (tempname.equalsIgnoreCase(jComboBox1.getItemAt(i).toString().split("\\]")[1].substring(3)))
	    		{
	    			nextstep = false;
	    			new ErrorDialog(14, JOptionPane.INFORMATION_MESSAGE, user);
	    			break;
	    		}
	    	}
	        	
	    	if (nextstep)
	    	{
	    		user.setId(Integer.parseInt(jComboBox1.getSelectedItem().toString().split("\\[")[1].split("\\]")[0]));
	    		user.setUsername(jTextField1.getText());
			    user.setPassword(jTextField3.getText());
				if (jRadioButton1.isSelected()){
					user.setRole("user");
				}
				else{
					user.setRole("admin");
				}
				if (jRadioButton3.isSelected()){
					user.setLanguage(Locale.GERMAN);
				}
				else{
					user.setLanguage(Locale.ENGLISH);
				}
		        
		        // Aenderung ueber den Server speichern
	     		sc = new ServerConnection();
	     		Response resp = sc.sendRequest(new Request(this.user,"adminSaveUserProfile"));
	     		if (resp == null)
	     			System.out.println(user.displayValue("mw82"));
	     		else if (resp.getContent() instanceof Boolean){
	     			boolean ok = (Boolean) resp.getContent();
	     			if (ok){
	     				this.dispose();
	     			}
	     			else{
	     				new ErrorDialog(4, JOptionPane.INFORMATION_MESSAGE, user);
	     			}
	     		}
	     		else if (resp.getContent() == null ){
	     			new ErrorDialog(5, JOptionPane.ERROR_MESSAGE, user);
	     		}
	     		else
	     			System.out.println(resp.getContent().toString());
	     		//CategoryAccessLayer ual = new CategoryAccessLayer();
	     		//ual.setCategory(cat);
	    	}
       }
   	else{
   		new ErrorDialog(0, JOptionPane.ERROR_MESSAGE, user);
   	}
   }


   
   // Variables declaration - do not modify
   javax.swing.DefaultComboBoxModel cbmodel;
   private ServerConnection sc;
   private User user;
   private User[] users;
   private javax.swing.ButtonGroup buttonGroup1;
   private javax.swing.ButtonGroup buttonGroup2;
   private javax.swing.ButtonGroup buttonGroup3;
   private javax.swing.ButtonGroup buttonGroup4;
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JComboBox jComboBox1;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JLabel jLabel4;
   private javax.swing.JRadioButton jRadioButton1;
   private javax.swing.JRadioButton jRadioButton2;
   private javax.swing.JRadioButton jRadioButton3;
   private javax.swing.JRadioButton jRadioButton4;
   private javax.swing.JTextField jTextField1;
   private javax.swing.JTextField jTextField3;
   // End of variables declaration
}
